<?php
namespace app\api\controller;
use think\Db;
use app\api\controller\Base;


class Adds extends Base
{
    /*
     * 方法作用：发布产品的添加
     * @param
     * */
    public function adds(){
            $uid=$this->uid;
      		//$mid=input('mid');
            $sql=db('user')
                ->where("uid",$uid)
                ->field('uflg')
                ->find();
            if ($sql['uflg']==0){
                return $this->AjaxReturn("123","黑名单无权操作");
            }
            $times=date('Y-m-d 00:00:00');
            $timee=date('Y-m-d H:i:s');
            $where ['createtime'] = array("between",array($times,$timee));
      		$where['uid']=$uid;
           $res=db('message')
                ->where($where)
                ->count();
      		$k=db('shezhi')
              ->find();
            $rs=db('real')
                ->where("uid",$uid)
             	->field('rid,rflg')
                ->find();
            if($rs['rflg']!="1"&&$res>=$k['userwenzhang']){
                return $this->AjaxReturn("12","今日超出发布次数");
            }
      		if($rs['rflg']=="1"&&$res>=$k['vipwenzhang']){
                return $this->AjaxReturn("22","今日超出发布次数");
            }                                
            $timelimit=input('timelimit');
            $value=input("timelimit")*3600*24+time();
      		$val=date("Y-m-d H:i:s",$value);
            $date['title']=input('title');
            $date['content']=input('content');
            $date['createtime']=date("Y-m-d H:i:s");
            $date['stoptime']=$val;
     		$date['address']=input('address/a');
            $date['address'] = implode('',$date['address']);
            $date['uid']=$uid;
            $date['type']=1;
            $date['number']=input('number');
      
            $date['price']=input('price');
      		
      		if(empty($date['price'])){
            	$date['price']="商议"; 
            }
            $date['unit']=intval(input('unit'));
            $date['timelimit']=input('timelimit');
			//return $this->AjaxReturn('456','ceshi',$date['stoptime']);
			$add=db('message')
                ->insertGetId($date);
      		//print_r($add);die;
            if ($add){
                return $this->AjaxReturn('1','发布成功',$add);
            }else{
                return $this->AjaxReturn('0','发布失败');
            }
        }

    /*
     * 方法作用：发布文章的添加
     * @param
     * */
     public function Based()
    {
       
        $file = request()->file('file');
        $mid=input('mid');
        $filename=md5(time().rand(1,1000));
        $fileRoute = ROOT_PATH . 'upload/message/'; 
        $info = $file->move($fileRoute,$filename);
        if (!$info){
            return $this->AjaxReturn('10','失败',1);
        }
      
        $type = input('type');
        if($mid==''){
         	$where['type']=$type;
         	$where['picture']=$info->getFilename().',';
         	//$where['createtime']=date("Y-m-d H:i:s");
        	$arr=db('message')
              ->insertGetid($where);
           
         	return $this->AjaxReturn('1','操作成功',[$arr,$info->getFilename()]);	
        }else{
          
           $res= db('message')
              ->where('mid',$mid)
              ->field('picture')
              ->find();
         $where['picture']= $res['picture'].$info->getFilename().',';
          
          $arr= db('message')
              ->where('mid',$mid)
              ->update($where);
       }
        if($arr){
            return $this->AjaxReturn('1','操作成功',[$mid,$info->getFilename()]);
        }else{
            return $this->AjaxReturn('1','操作失败',$mid);
        }
    }
  		//Base64图片上传
    public function Base()
    {
      	$mid=input('mid');
        //接收用户uid
        $uid=$this->uid;
        //接收最新的图片
        $image=input('image');
      	$image=explode(',',$image);
      	$image=array_filter($image);
      	foreach($image as $v){
            //服务器中存放图片的文件夹名称
            $path='message/';
            //生成一个随机数用来生成图片名称
            $rand=rand(1000,9999);
            //生成图片名称
            $pers=md5(time().$rand).'.jpg';
            //文件夹目录
            $path ='upload'.DS.$path;
            //文件绝对路径
            $absolute=ROOT_PATH.$path.DS.$pers;
            //判断目录是否存在 不存在就创建
            if (!is_dir(ROOT_PATH.$path))
            {
                mkdir(ROOT_PATH.$path,0777,true);
            }

               $res= db('message')
                  ->where('mid',$mid)
                  ->field('picture')
                  ->find();
             $where['picture']= $res['picture'].$pers.',';
              $arr= db('message')
                  ->where('mid',$mid)
                  ->update($where);
            //将图片上传到服务器上
            $r = file_put_contents($absolute, base64_decode($v));
          }
          	return $this->AjaxReturn('1','成功');

    }
        public function addwenzhang(){
            $uid=$this->uid;
            $sql=db('user')
                ->where("uid",$uid)
                ->find();
            if ($sql['uflg']==0){
                return $this->AjaxReturn("0","黑名单无权操作");
            }
            $times=date('Y-m-d 00:00:00');
            $timee=date('Y-m-d H:i:s');
            $where ['createtime'] = array("between",array($times,$timee));
            $where['uid']=$uid;
            $res=db('message')
                ->where($where)
                ->count();
          	//return $this->AjaxReturn("22","今日超出发布次数",$res);
            $rs=db('real')
                ->where("uid",$uid)
              	->field('rflg')
                ->find();
            //return $this->AjaxReturn("22","今日超出发布次数",$rs);
          	$k=db('shezhi')
            ->find();//return $this->AjaxReturn("22","今日超出发布次数",$k['vipwenzhang']);
              if($rs['rflg']!="1"&&$res>=$k['userwenzhang']){
                return $this->AjaxReturn("12","今日超出发布次数");
            }
      		if($rs['rflg']=="1"&&$res>=$k['vipwenzhang']){
                return $this->AjaxReturn("22","今日超出发布次数");
            }                                
            $date['timelimit']=input('timelimit');
            $value=input("timelimit")*3600*24+time();
            $date['title']=input('title');
            $date['content']=input('content');
            $date['createtime']=date("Y-m-d H:i:s");
            $date['stoptime']="3019-09-09 23:59:59";
            $date['uid']=$uid;
            $date['type']=2;
          	$date['price']='';
          	$date['unit']='';
          	$date['number']='';
            $date['faxian']=0;
            $date['address']=input('address');
            $date['timelimit']=input('timelimit');
          	
              $add=db('message')
                  ->insertGetId($date);
            
            
            if ($add){
                return $this->AjaxReturn('1','发布成功',$add);
            }else{

                return $this->AjaxReturn('0','发布失败');
            }
        }
      
/*
 * 方法作用：行情资讯列表页面
 * @param $whole string 查询条件
 * @return code msg array[]   状态码  提示信息 返回数据
 
 * */
    public function InformationList(){
        $uid=$this->uid;
      	$page=input('page');
        $page=($page-1)*6;

            $sql=db('zixun')
                ->where('flg',0)
              	->order('createtime desc')
                ->limit($page,6)
                ->select();
            foreach ($sql as $key=>$value){
                $value['prctures']="https://www.mamingwang.cn" .$value['picture'];
                $arr[]=$value;

            }
            if (!empty($sql)){
                return $this->AjaxReturn("-1","该用户未实名认证",$arr);
            }else{
                return $this->AjaxReturn("-2","操作失败");
            }



    }
    /*
     * 方法作用：行情资讯搜索
     * @param $whole string 查询条件
     * @return code msg array[]   状态码  提示信息 返回数据
     * */
    public function InformationSee(){
        $whole=input('text');
        $where ['title'] = array("like","%".$whole."%");
        $wheres ['content'] = array("like","%".$whole."%");
        $sql=db('zixun')
            ->where($where)
            ->whereOr($wheres)
            ->where('flg',0)
            ->select();
        //print_r($sql);die;
       foreach ($sql as $key=>$value){
            $value['prctures']='https://www.mamingwang.cn' .$value['picture'];
            $arr[]=$value;
           
        }
        if (!empty($sql)){
            return $this->AjaxReturn("1","操作成功",$arr);
        }else{
            return $this->AjaxReturn("-1","操作失败");
        }
    }
    /*
     *方法作用：行情资讯详细页面
     * @param $id int 接收过来的文章ID
     *@param $where array 查询条件
     * @return code msg array[]   状态码  提示信息 返回数据
     * */
    public function InformationCheck(){
        $uid=$this->uid;
        $zid=input('id');
        $rs=db('real')
            ->where('uid',$uid)
          	->field('rflg')
            ->find();
        $re=db('user')
            ->where('uid',$uid)
            ->field('gflg')
            ->find();
        if ($rs['rflg']==1||$re['gflg']==1)
        {
            $check=db('zixun')
                ->where('zid',$zid)
                ->find();
            $check['picture']='https://www.mamingwang.cn'. $check['picture'];
            //浏览量加1
            $num=db('zixun')->where('zid',$zid)->field('id')->find();
            $arr8=array_filter(explode(',',$num['id']));
            //print_r($arr8);die;
            if(!in_array($uid,$arr8))
            {
                $num1=$num['id'].$uid.',';
                $update=db('zixun')->where('zid',$zid)->update(['id'=>$num1]);
            }
            if (!empty($check))
            {
                return $this->AjaxReturn("1","操作成功",[$check,$rs]);
            }
            else
            {
                return $this->AjaxReturn("0","操作失败");
            }
        }
        else
        {
            $res=db('zpay')
                ->where('uid',$uid)
                ->where('zid',$zid)
                ->field('flg')
                ->select();
            $arr=array();
            foreach ($res as $key=>$value)
            {
                $arr[]=$res[$key]['flg'];
            }
            //print_r($arr);die;
            if(in_array("已支付",$arr))
            {
                $check=db('zixun')
                    ->where('zid',$zid)
                    ->find();
                $check['picture']='https://www.mamingwang.cn'. $check['picture'];
                $num=db('zixun')->where('zid',$zid)->field('id')->find();
                $arr8=array_filter(explode(',',$num['id']));
                return $arr8;die;
                if(!in_array($uid,$arr8))
                {
                    $num1=$num['id'].$uid.',';
                    $update=db('zixun')->where('zid',$zid)->update(['id'=>$num1]);
                }
                if (!empty($check))
                {
                    return $this->AjaxReturn("1","操作成功",[$check,$rs]);
                }
                else
                {
                    return $this->AjaxReturn("0","操作失败");
                }
            }
            else
            {
                $check=db('zixun')
                    ->where('zid',$zid)
                    ->find();
                $check['picture']='https://www.mamingwang.cn'. $check['picture'];
                return $this->AjaxReturn("22","无权查看",[$check,$rs]);
            }
        }

    }
    /*
     *方法作用：委托采购信息
     * @param $whole int 咨询或者在线预约
     * @return code msg array[]   状态码  提示信息 返回数据
     * */
    public function EntrustPurchase(){
        $sql=db('caigous')
            ->select();
        if (!empty($sql)){
            return $this->AjaxReturn("1","操作成功",$sql);
        }else{

            return $this->AjaxReturn("1","操作成功");
        }
    }
    /*
     *方法作用：点击委托采购
     * @param $whole int 咨询或者在线预约
     * @return code msg array[]   状态码  提示信息 返回数据
     * */
    public function Entrus(){
        $uid=$this->uid;
      	$flg=input('flg');
        $where['uid']=$uid;
        $where['flg']=$flg;
        $where['createtime']=date("Y-m-d H:i:s") ;
      	$sr=db('user')
          ->where('uid',$uid)
          ->find();
      	if($sr['uflg']==0){
        	return $this->AjaxReturn('123','黑名单无权操作');
        }
        $sql=db('caigou')
            ->insert($where);
        if (!empty($sql)){
            return $this->AjaxReturn('1',"操作成功");
        }else{
            return $this->AjaxReturn('0',"操作失败");
        }
    }
    /*
     * 方法作用 ：金融服务随你呀部分
     * @param $whole int 咨询或者在线预约
     * @param $sql array[] 随你压信息
     * @return code msg array[]   状态码  提示信息 返回数据
     */
    public function Financialsuiya(){
        $sql=db('suiyas')
            ->select();

        if (!empty($sql)){
            return $this->AjaxReturn("1","操作成功",$sql);
        }else{
            return $this->AjaxReturn("0","操作失败");
        }
    }
    /*
     * 方法作用 ：金融服务帮你踩显示部分
     * @param $whole int 咨询或者在线预约
     * @param $res array[] 帮你踩信息
     * @return code msg array[]   状态码  提示信息 返回数据
     */
    public function Financialbangcai(){
        $sql=db('bangcais')
            ->select();
        if (!empty($sql)){
            return $this->AjaxReturn("1","操作成功",$sql);
        }else{
            return $this->AjaxReturn("0","操作失败");
        }
    }
    /*
     * 方法作用 ：点击随你呀
     * @param $whole int 咨询或者在线预约
     * @param $sql array[] 随你压信息
     * @param $res array[] 帮你踩信息
     * @return code msg array[]   状态码  提示信息 返回数据
     */
    public function suiniya(){
        $uid=$this->uid;
        $where['uid']=$uid;
      	$sr=db('user')
          ->where('uid',$uid)
          ->find();
      	if($sr['uflg']==0){
        	return $this->AjaxReturn('123','黑名单无权操作');
        }
        $where['createtime']=date("Y-m-d H:i:s");
        $where['flg']=input('flg');
        $res=db('suiya')
            ->insert($where);
        if(!empty($res)){
            return $this->AjaxReturn("1","操作成功");
        }else{
            return $this->AjaxReturn("0","操作失败");
        }
    }
    /*
    * 方法作用 ：点击帮你踩
    * @param $whole int 咨询或者在线预约
    * @param $sql array[] 随你压信息
    * @param $res array[] 帮你踩信息
    * @return code msg array[]   状态码  提示信息 返回数据
    */
    public function bangnicai(){
       $uid=$this->uid;
        $where['uid']=$uid;
      	$sr=db('user')
          ->where('uid',$uid)
          ->find();
      	if($sr['uflg']==0){
        	return $this->AjaxReturn('123','黑名单无权操作');
        }
        $where['createtime']=date("Y-m-d H:i:s");
        $where['flg']=input('flg');
        $res=db('bangcai')
            ->insert($where);
        if(!empty($res)){
            return $this->AjaxReturn("1","操作成功");
        }else{
            return $this->AjaxReturn("0","操作失败");
        }
    }
    public function del(){
            $mid=input('mid');
            $photo=input('photo');
            $res=db('message')
              ->where('mid',$mid)
              ->field('picture')
              ->find();
        $arr=explode(",",$res['picture']);
      	$arr=array_filter($arr); 
			for($i=0;$i<count($arr);$i++){
              if($arr[$i]==$photo){
              	$arr[$i]='';
              }
            }

      	$rs=implode(",",$arr);
      	$stu=db('message')
          ->where('mid',$mid)
          ->update(['picture'=>$rs]);
      	
      	$fileRoute = ROOT_PATH . 'upload/message/'.$photo;
      	//unlink($fileRoute);
     	if(unlink($fileRoute)){
          return $this->AjaxReturn('1','成功',[$res,$fileRoute]);
    	}else{
        	return $this->AjaxReturn('2','失败');
        }
	}
    public function fangqi(){
      $mid=input(mid);
      $res=db()
        ->delete('mid',$mid);
      if($res){
          return $this->AjaxReturn('1','放弃发布');
      }
    } 
  	public function delsql(){
    	$mid= input('mid');
      	if($mid != ''){
      	$res= db('message')
          ->where('mid',$mid)
          ->find();
      if($res['createtime']==''){
         $arr=explode(",",$res['picture']);
         $arr = array_filter($arr);
      	 for($i=0;$i<count($arr);$i++){
         	$fileRoute = ROOT_PATH . 'upload/message/'.$arr[$i];
        	unlink($fileRoute);
    	}
         $rs=db('message')
           ->where('mid',$mid)
            ->delete();
      }
      return $this->AjaxReturn('10','成功');
    	}
	}
}